﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://wcfextension.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace WcfExtension
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;

    internal class LocalLogService : IWcfLogService
    {
        private static object locker = new object();
        static LocalLogService()
        {
            var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wcflog");
            if (!Directory.Exists(logPath))
                Directory.CreateDirectory(logPath);
            Debug.Listeners.Add(new TextWriterTraceListener(logPath + "/wcflog" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));
            Debug.AutoFlush = true;
        }

        public static void Log(string text)
        {
            lock (locker)
            {
                Debug.WriteLine("================================== START ======================================");
                Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Debug.WriteLine(text);
                Debug.WriteLine("=================================== END =======================================");
            }
        }

        public string Health()
        {
            throw new NotImplementedException("Local log service does not need to support this!");
        }

        public void Log(List<AbstractLogInfo> logInfo)
        {
            Debug.WriteLine("================================== START ======================================");
            Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + logInfo.First().GetType().Name);
            logInfo.ForEach(item => Debug.WriteLine(item));
            Debug.WriteLine("=================================== END =======================================");
        }

        public void Log(AbstractLogInfo logInfo)
        {
            Log(new List<AbstractLogInfo> { logInfo });
        }
    }
}
